// model.js
/**
 * model.js 三级联动js
 * 模板默认是广东省广州市
 * @author $wp 2018-04-09 15:26
 */
var area = require('../utils/area.js')

var areaInfo = []; //所有省市区县数据

var provinces = []; //省

var citys = []; //城市

var countys = []; //区县

var value = [0, 0, 0]; //数据位置下标

var info = {};
var vali = [];

function getAreaByData(that, a, b, c) {
  //获取省份
  provinces = [];
  for (var i = 0; i < areaInfo.length; i++) {
    if (areaInfo[i].parentid == "0" && areaInfo[i].name == a) {
      provinces[0] = areaInfo[i];
    }
  }
  citys = [];
  for (var j = 0; j < areaInfo.length; j++) {
    if (areaInfo[j].level == 2 && areaInfo[j].name == b) {
      citys[0] = areaInfo[j];
    }
  }
  //获取区县
  countys = [];
  for (var k = 0; k < areaInfo.length; k++) {
    if (areaInfo[k].name == c && areaInfo[k].level == 3) {
      countys[0] = areaInfo[k];
    }
  }
  that.setData({
    item: {
      provinces: provinces,
      citys: citys,
      countys: countys
    }
  })

}

function updateAreaData(that, status, e, address) {
  var a = '北京市',
    b = '北京市',
    c = '';
  if (address != '' && address != null && address != undefined) {
    a = address.a;
    b = address.b;
    c = address.c;
  }
  //获取省份数据
  var getProvinceData = function () {
    var s;
    provinces = [];
    var num = 0;
    for (var i = 0; i < areaInfo.length; i++) {
      s = areaInfo[i];
      if (s.parentid == "0") {
        provinces[num] = s;
        num++;
      }
    }

    //获取当前省份的索引值 广东省 可改成自己当前的省份
    provinces.some((item, index) => {
      if (item.name.indexOf(a) != -1) {
        vali[0] = index;
        return;
      }
    })

    //初始化调一次传递索引值，传递省城数据
    //获取地级市数据
    getCityArr(vali[0]);

    //获取当前城市的索引值 广州市 可改成自己当前的城市
    citys.some((item, index) => {
      if (item.name.indexOf(b) != -1) {
        vali[1] = index;
        return;
      }
    })
    if (c == '' || c == null || c == undefined) {
      //获取区县数据
      getCountyInfo(vali[0], vali[1]);
    } else {
      //获取区县数据,带参数
      getCountyInfo(vali[0], vali[1], c);
    }

    //赋值
    info = {
      item: {
        provinces: provinces,
        citys: citys,
        countys: countys,
        value: value
      }
    }

    animationEvents(that, 200, false, 0);
  }

  // 获取地级市数据
  var getCityArr = function (count = 0) {
    var c;
    citys = [];
    var num = 0;
    for (var i = 0; i < areaInfo.length; i++) {
      c = areaInfo[i];
      if (c.level == 2 && c.parentid == provinces[count].id) {
        citys[num] = c;
        num++;
      }
    }
    if (citys.length == 0) {
      citys[0] = {
        name: ''
      };
    }
  }

  // 获取区县数据
  var getCountyInfo = function (column0 = 0, column1 = 0, column2 = 0) {
    var c;
    countys = [];
    var num = 0;
    var flag = 0;
    for (var i = 0; i < areaInfo.length; i++) {
      c = areaInfo[i];
      if (column2 != 0) {
        if (c.level == 3 && c.parentid == citys[column1].id) {
          countys[num] = c;
          num++;
          if (c.name == column2) {
            flag = num - 1;
          }
        }
      } else {
        if (c.level == 3 && c.parentid == citys[column1].id) {
          countys[num] = c;
          num++;
        }
      }
    }
    if (countys.length == 0) {
      countys[0] = {
        name: ''
      };
    }
    value = [column0, column1, flag];
    console.log(value)
  }

  //滑动事件
  var valueChange = function (e, that) {
    var val = e.detail.value
    //判断滑动的是第几个column
    //若省份column做了滑动则定位到地级市和区县第一位
    if (value[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
      getCityArr(val[0]); //获取地级市数据
      getCountyInfo(val[0], val[1]); //获取区县数据
    } else { //若省份column未做滑动，地级市做了滑动则定位区县第一位
      if (value[1] != val[1]) {
        val[2] = 0;
        getCountyInfo(val[0], val[1]); //获取区县数据
      }
    }
    value = val;

    assignmentData(that, that.data.item.show)

  }

  if (status == 0) {
    area.getAreaInfo(function (arr) {
      areaInfo = arr;
      //获取省份数据
      getProvinceData();

    });
  }
  //滑动事件
  else {
    valueChange(e, that);
  }

}

//动画事件
function animationEvents(that, moveY, show, duration) {
  that.animation = wx.createAnimation({
    transformOrigin: "50% 50%",
    duration: duration,
    timingFunction: "ease",
    delay: 0
  })
  that.animation.translateY(moveY + 'vh').step()
  //赋值
  assignmentData(that, show)

}

//赋值
function assignmentData(that, show) {
  that.setData({
    item: {
      animation: that.animation.export(),
      show: show,
      provinces: provinces,
      citys: citys,
      countys: countys,
      value: value
    }
  })
}

module.exports = {
  updateAreaData: updateAreaData,
  getAreaByData: getAreaByData,
  animationEvents: animationEvents
}